package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"rock/customer/dto"
	"rock/customer/initParam"
	"rock/customer/middleware"
)

// @Summary 清除用户再融云上多余的群
// @Description
// @Tags 测试
// @ID /dismissallgroup
// @Accept json
// @Produce json
// @Param polygon body dto.User true "body"
// @Success 200 {object} middleware.Response{} "success"
// @Router /dismissallgroup [post]
func (t *TestController) dismissAllGroup(router *gin.Context) {
	params := &dto.User{}
	if err := params.BindingParams(router); err != nil {
		middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
		return
	}
	userId := params.UserId
	//rcParam := fmt.Sprintf("userId=%s", params.UserId)
	if params.IsCustomerClient == 1 {
		userId = initParam.CTarget + userId
	} else {
		userId = initParam.CsTarget + userId
	}
	rcParam := fmt.Sprintf("userId=%s", userId)
	rcReq, err := queryRonGroup(rcParam)
	if err != nil {
		middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
		return
	}
	groupids := make([]string, 0)
	for _, model := range rcReq.Groups {
		groupids = append(groupids, model.Id)
	}
	fmt.Println(groupids)
	userId = initParam.CsTarget + userId
	for _, v := range groupids {
		group, err := groupService.Group(router, BusinessDB, v)
		if err != nil {
			dealRequestError(router, err)
			return
		}
		if group.GroupID != "" {
			continue
		}
		rcParam := fmt.Sprintf("userId=%s&groupId=%s", userId, v)
		err = dismissGroupRonCloud(rcParam)
		if err != nil {
			fmt.Println("【RON CLOUD REQUEST ERROR】", err)
			middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
		}
		fmt.Println("【RON CLOUD REQUEST SUCCESS】", v)
	}
	middleware.ResponseSuccess(router, gin.H{"msg": initParam.DealSuccessText})
}
